package NewTmas.EditMode;

import NewTmas.Graphs.GraphVizGraph;
import att.grappa.Node;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

/**
 * Created by IntelliJ IDEA.
 * User: abhatnag
 * Date: 8/10/11
 * Time: 5:35 PM
 * To change this template use File | Settings | File Templates.
 */
public class AddTaskAfterListener implements ActionListener
{


    public AddTaskAfterListener(String jobname, GraphVizGraph graphVizGraph, Connection connection, EditIntFrame editIntFrame)
    {
        this.jobname = jobname;
        this.graphVizGraph = graphVizGraph;
        this.connection = connection;
        this.editIntFrame = editIntFrame;
    }

    String jobname;
    GraphVizGraph graphVizGraph;
    Connection connection;
    EditIntFrame editIntFrame;


    public void actionPerformed(ActionEvent e)
    {

        Node[] nodes = graphVizGraph.getGraph().nodeElementsAsArray();
        String[] currentNodeNames = new String[nodes.length];
        for (int i = 0; i < nodes.length; i++)
        {
            currentNodeNames[i] = nodes[i].getName();

        }

        JComboBox predcbox1 = new JComboBox(currentNodeNames);


        JComboBox newTaskCbox = new JComboBox(currentNodeNames);
        JTextField descField = new JTextField();

        Object[] message = new Object[]{"Predecessor Task_Tag", predcbox1, "New Task", newTaskCbox, "Description", descField};
        int input = JOptionPane.showConfirmDialog(null, message, "Add a New Task", JOptionPane.OK_CANCEL_OPTION);
        if (input == JOptionPane.OK_OPTION)
        {
            String predescessorTask = predcbox1.getSelectedItem().toString();
            String newTask = newTaskCbox.getSelectedItem().toString();
            String description = descField.getText();

            CallStoredProc storedProc = new CallStoredProc("{call tms.task_util.add_after(?,?,?,?)}", new String[]{jobname, newTask, predescessorTask, description}, connection);

            editIntFrame.sqlCommands.add(storedProc);
            editIntFrame.model.addElement(storedProc);
            editIntFrame.sqlcommandsList.setModel(editIntFrame.model);

        }

    }


}
